package com.adp.interceptor.roworder;

import com.adf.config.Config;
import com.adf.config.component.application.report.ReportBean;
import com.adf.system.ReportRequest;
import com.adf.system.commoninterface.IListReportRoworderPersistence;
import com.adf.util.Consts;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;


public class resListStoreRowOrderByDrag implements IListReportRoworderPersistence {

	public String loadRoworder(Map<String, String> mColValuesInRow) {
		 return mColValuesInRow.get("ORDERLINE");
	}

	public void storeRoworderByArrow(ReportRequest arg0, ReportBean arg1,
                                     Map<String, String> arg2, Map<String, String> arg3, boolean arg4) {
	}

	public void storeRoworderByDrag(ReportRequest rrequest, ReportBean rbean,
                                    Map<String, String> mColValuesInRow, Map<String, String> mColValuesInDestRow, boolean direct) {
		/**
         * 因为no、orderline两个<col/>都配置了rowordervalue为true，所以可以从中取到它们的值
         */
        String PID=mColValuesInRow.get("PID");
        String ID=mColValuesInRow.get("RECID");
        String myOrderline=mColValuesInRow.get("ORDERLINE");//取到被排序记录行的no列的值（因为column为no的<col/>的rowordervalue属性值为true）
        String destOrderline=mColValuesInDestRow.get("ORDERLINE");//取到目标位置排序列的值
        int idestOrderline;
        int iret = 0;
        try
        {
            idestOrderline=Integer.parseInt(destOrderline);
        }catch(NumberFormatException e1)
        {
            idestOrderline=0;
        }
        Connection conn=null;
        Statement stmt=null;
        try
        {
            conn= Config.getInstance().getDataSource(rbean.getSbean().getDatasource()).getConnection();
            stmt=conn.createStatement();
            ResultSet rs=null;
            int iorderline=0;
            String sql="";
            if(direct)
            {//如果是移到目标记录行的上方
            	//取到目标记录行上面一行的排序值，稍后要移到它们中间
            	sql=String.format("update TBL_S_RESOURCE_INFO set  orderline=orderline+1 where pid='%s' and (orderline>=%s and orderline<%s)", PID, destOrderline, myOrderline);
           
            }else
            {//移到被排序记录行的下方
            	sql=String.format("update TBL_S_RESOURCE_INFO set  orderline=orderline-1 where pid='%s' and (orderline<=%s and orderline>%s)", PID, destOrderline, myOrderline);
            }
            iret = stmt.executeUpdate(sql);
            sql = String.format("update TBL_S_RESOURCE_INFO set orderline=%s where RECID='%s'", destOrderline, ID);
            iret = stmt.executeUpdate(sql);
            rrequest.getWResponse().getMessageCollector().success("排序记录行数据成功");
        }catch(SQLException e)
        {
            e.printStackTrace();
            rrequest.getWResponse().setStatecode(Consts.STATECODE_FAILED);
            rrequest.getWResponse().getMessageCollector().warn("排序报表记录行失败",null,true);
        }
		
	}

	public void storeRoworderByInputbox(ReportRequest arg0, ReportBean arg1,
                                        Map<String, String> arg2, String arg3) {
	}

	public void storeRoworderByTop(ReportRequest arg0, ReportBean arg1,
                                   Map<String, String> arg2) {
	}
	
	
}
